package com.study.mq;

import com.rabbitmq.client.*;
import lombok.SneakyThrows;

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * 关闭AutoAck实现"能者多劳"
 */
public class MqConsumer2 {
	public static void main(String[] args) throws IOException, TimeoutException {
		Connection connection = MqUtils.getConnection();

		Channel channel = connection.createChannel();

		channel.basicQos(1);

		channel.basicConsume("hello", false, new DefaultConsumer(channel){
			@SneakyThrows
			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
				TimeUnit.SECONDS.sleep(1);
				System.out.println(new String(body));
				//false,应答一个?
				channel.basicAck(envelope.getDeliveryTag(),false);
			}
		});
	}
}
